package fit.deni.hibernate;

import java.util.Collection;

import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import fit.deni.beans.Zanr;
import fit.deni.hibernate.interfaces.ZanrService;

public class ZanrServiceImpl implements ZanrService {

	private Session session;

	public ZanrServiceImpl(Session session) {
		this.session = session;
	}

	public Collection<Zanr> getAll() {
		return session.createCriteria(Zanr.class).add(Restrictions.eq("deleted", false)).list();
	}

	@CommitAfter
	public void addGenre(Zanr zanr) {
		session.merge(zanr);
	}

	public Zanr getGenreById(int id) {
		return (Zanr) session.createCriteria(Zanr.class).add(Restrictions.eq("id", id)).list().get(0);
	}

	@CommitAfter
	public void deleteGenre(Zanr zanr) {
		zanr.setDeleted(true);
		session.merge(zanr);
//		session.delete(zanr);
//		session.flush();
	}

	public Zanr getGenreByName(String name) {
		return (Zanr) session.createCriteria(Zanr.class).add(Restrictions.eq("tip", name)).list().get(0);
	}

}
